#include "localizationwindow.h"
#include "ui_localizationwindow.h"

LocalizationWindow::LocalizationWindow(QWidget *parent,ArClientBase *client) :
    QDialog(parent),
    ui(new Ui::LocalizationWindow),
    getLocStateCB(this,&LocalizationWindow::getLocState)
{
    ui->setupUi(this);

    cliente = client;


    if(cliente->dataExists("getLocState"));
    {
        cliente->addHandler("getLocState", &getLocStateCB);
        cliente->request("getLocState",1000);
    }

     this->setFixedSize(this->geometry().width(),this->geometry().height());
    this->setAttribute(Qt::WA_DeleteOnClose);

}

LocalizationWindow::~LocalizationWindow()
{
    cliente->remHandler("getLocState", &getLocStateCB);
    delete ui;
}

void LocalizationWindow::on_button_done_clicked()
{
    ui->button_localize->setChecked(false);
    emit signal_changed_localization_button(false);
    this->hide();
}

void LocalizationWindow::on_button_localize_clicked(bool checked)
{
    emit signal_changed_localization_button(checked);
}

void LocalizationWindow::slot_changed_localization_button(bool checked){
    ui->button_localize->setChecked(checked);
}


void LocalizationWindow::getLocState(ArNetPacket *packet){

    int state;
    float score;

    state = packet->bufToUByte();
    score = packet->bufToUByte2();

    score /= 10.0;

    //char *name = cliente->getHost();


    QString men;
    men.append("Robot ");
    men.append(cliente->getHost());
    men.append(" is ");
    switch(state){
    case 0:
        men.append(" localized.");
        break;
    case 1:
        men.append(" mapping.");
        break;
    case 2:
        men.append(" lost.");
        break;
    case 3:
        men.append(" locating.");
        break;
    default:
        men.append(" uncertain.");
        break;
    }

    ui->label_status->setText(men);

    men.clear();

    men.append("Localization score:  ");
    men.append(QString::number(score));
    men.append("%");

    ui->label_score->setText(men);

}

//     "ubyte: 0 == initialized (good), 1 == idle (probably mapping), 2 == uninitialized (lost), 3 == initializing (someone tried to localize, can't move until its done); ubyte2: locScore * 1000 (0 == lost, 1000 = perfect match)",
